﻿Imports MindFusion.Scheduling
Imports MindFusion.Scheduling.WinForms
Imports System.Drawing.Drawing2D

Public Class frmMoniteursSEM
    Public pCD_MON As String

    Private szSQL As String
    Private rs As ADODB.Recordset
    Private centered As StringFormat
    Dim xDate As New Date(2000, 5, 1, 0, 0, 0)

    Private Sub frmMoniteursPLAN_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Me.Text = "PLANNING : " & pCD_MON
        ' définir l'orientation du texte
        centered = New StringFormat
        centered.Alignment = StringAlignment.Center
        centered.LineAlignment = StringAlignment.Center

        ' initialiser la date à lundi 01/05/2000
        ' affichage sur 6 jours
        CAL_PLANNING.CurrentView = CalendarView.Timetable
        CAL_PLANNING.GroupType = GroupType.None
        CAL_PLANNING.TimetableSettings.Dates.Clear()
        CAL_PLANNING.Date = xDate
        For i = 0 To 5
            CAL_PLANNING.TimetableSettings.Dates.Add(CAL_PLANNING.Date.AddDays(i))
        Next i
        CAL_PLANNING.EndDate = CAL_PLANNING.Date.AddDays(5)

        CAL_PLANNING.CurrentTime = Now
        ' affichage à la journée
        LoadPlanning()

    End Sub

    Sub LoadPlanning()
        Dim noResource As Integer = 0

        Me.Cursor = Cursors.WaitCursor
        ' effacer la sélection
        CAL_PLANNING.Selection.Reset()
        CAL_PLANNING.ResetDrag()
        ' effacer le calendrier
        CAL_PLANNING.Schedule.Clear()
        CAL_PLANNING.Resources.Clear()

        Dim resource As Resource = Nothing

        CAL_PLANNING.BeginInit()
        ' Affichage par semaine, disposer les heures en coordonnées
        LoadRendezVous(CAL_PLANNING.Date, CAL_PLANNING.EndDate, pCD_MON, noResource)
        CAL_PLANNING.EndInit()
        ' positionner le planning sur 6h
        CAL_PLANNING.VScrollPos = 5
        Me.Cursor = Cursors.Default

    End Sub

    Private Sub LoadRendezVous(ByVal debut As Date, ByVal fin As Date, ByVal moniteur As String, ByVal noResource As Integer)
        Dim rsRDV As ADODB.Recordset = Nothing
        Dim app As Appointment

        ' Lire la journée du planning
        szSQL = "CALL spGet_MONITEURS_bySEMAINE(" & gUserAgence & ",'" & moniteur & "','" & _
                 Format(debut, "yyyy/MM/dd") & "','" & Format(fin, "yyyy/MM/dd") & "')"
        GetRecordset(ctDSN_AEM, szSQL, rsRDV)

        Do While Not rsRDV.EOF
            app = New Appointment()
            app.StartTime = rsRDV.Fields("dh_debut").Value
            app.EndTime = app.StartTime.AddHours(rsRDV.Fields("qte").Value)
            app.HeaderText = Mid(rsRDV.Fields("intitule").Value, 1, 15)
            app.Tag = rsRDV.Fields("activite").Value
            app.Id = rsRDV.Fields("no_ligne").Value

            ' colorier la cellule selon le statut du RDV
            Dim bcolor As MindFusion.Drawing.SolidBrush

            If app.Tag = 99995 Then
                ' heure = couleur blanc
                bcolor = New MindFusion.Drawing.SolidBrush(Color.White)
            Else
                ' activité = couleur bleu ciel
                bcolor = New MindFusion.Drawing.SolidBrush(Color.LightBlue)
            End If
            app.Style.Brush = bcolor
            app.AllowChangeEnd = True
            app.AllowChangeStart = True
            app.AllowMove = True
            app.AllDayEvent = False
            CAL_PLANNING.Schedule.Items.Add(app)
            rsRDV.MoveNext()
        Loop
        ' Close the recordset AND connection.
        rsRDV.Close()


    End Sub

    Private Sub CAL_PLANNING_DateClick(sender As Object, e As MindFusion.Scheduling.WinForms.ResourceDateEventArgs) Handles CAL_PLANNING.DateClick

        ' double-click ? 
        If e.Clicks = 2 And e.Button = System.Windows.Forms.MouseButtons.Left Then
            ' affichage journée, insérer nouveau rdv
            If IsUserAuthorised(2) = False Then
                Exit Sub
            End If
            ' afficher la ligne pour creation
            frmMoniteursSEMD.pNoLigne = 0
            frmMoniteursSEMD.pCD_MON = pCD_MON
            frmMoniteursSEMD.pDateTime = e.Date
            frmMoniteursSEMD.ShowDialog()
            ' réaffiche la viewlist
            LoadPlanning()
        End If

    End Sub

    Private Sub CAL_PLANNING_Draw(sender As Object, e As MindFusion.Scheduling.WinForms.DrawEventArgs) Handles CAL_PLANNING.Draw

        Dim bounds As Rectangle = e.Bounds
        Dim g As MindFusion.Drawing.IGraphics = e.Graphics
        Dim r As Resource = e.Resource
        Dim Jour As Date = e.Date

        Select Case e.Element
            Case CustomDrawElements.ResourceViewRowHeader
                ' encadrer les nom des ressources
                g.FillRectangle(Brushes.White, e.Bounds)
                g.DrawString(r.Name, e.Style.HeaderFont, Brushes.Navy, _
                             New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), centered)
                g.DrawLine(Pens.Gray, e.Bounds.Left, e.Bounds.Bottom - 1, e.Bounds.Right - 1, e.Bounds.Bottom - 1)
                ' tracer une ligne sur bordure gauche
                g.DrawLine(Pens.Gray, e.Bounds.Right - 1, e.Bounds.Top, e.Bounds.Right - 1, e.Bounds.Bottom)

            Case CustomDrawElements.ResourceViewCell
                ' colorier la colonne
                Dim bcolor As Brush = Brushes.White
                If IsDayOff(r.Id, Now.Date) = True Then
                    ' jour de repos du moniteur ?
                    bcolor = Brushes.LightGray
                End If
                g.FillRectangle(bcolor, bounds)
                ' tracer une ligne sur bordure gauche
                g.DrawLine(Pens.Gray, e.Bounds.Right - 1, e.Bounds.Top, e.Bounds.Right - 1, e.Bounds.Bottom)
        End Select

        If CAL_PLANNING.CurrentView = CalendarView.Timetable Then
            bounds.Inflate(-1, -1)
            ' colorier les cellules selon les horaires : travail = jaune pale, non travail = fumée blanc
            If IsDayOff(pCD_MON, Now.Date.AddHours(e.StartTime.Hours)) = True Then
                ' jour de repos du moniteur ?
                e.Graphics.FillRectangle(Brushes.LightGray, bounds)
            Else
                e.Graphics.FillRectangle(Brushes.LightYellow, bounds)
            End If
        End If

    End Sub

    Private Sub CAL_PLANNING_ItemClick(sender As Object, e As MindFusion.Scheduling.WinForms.ItemMouseEventArgs) Handles CAL_PLANNING.ItemClick

        If gUserAcces < 4 Then
            Exit Sub
        End If

        If System.Windows.Forms.MouseButtons.Left Then
            ' Get the clicked event
            Dim app As Appointment = e.Item 'as Appointment
            ' double-click = Modifier
            If e.Clicks = 2 Then
                ' afficher les détails de la ligne NoLigne
                frmMoniteursSEMD.pNoLigne = app.Id
                frmMoniteursSEMD.pCD_MON = pCD_MON
                frmMoniteursSEMD.pDateTime = app.StartTime.Date
                frmMoniteursSEMD.ShowDialog()
                ' réaffiche la viewlist
                LoadPlanning()
            End If
        End If

    End Sub

    Private Sub CAL_PLANNING_ItemDeleting(sender As Object, e As MindFusion.Scheduling.WinForms.ItemConfirmEventArgs) Handles CAL_PLANNING.ItemDeleting

        e.Confirm = False

        If gUserAcces < 4 Then
            Exit Sub
        End If

        deleteEnregistrement("moniteurs_semaine", " l'activité " & e.Item.HeaderText, e.Item.Id)

    End Sub

    Private Sub CAL_PLANNING_ItemModified(sender As Object, e As MindFusion.Scheduling.WinForms.ItemModifiedEventArgs) Handles CAL_PLANNING.ItemModified

        Dim app As Appointment = e.Item 'as Appointment

        If gUserAcces < 4 Then
            Exit Sub
        End If

        ' mettre à jpur le rendez-vous suite à un move ou resize 
        szSQL = "UPDATE moniteurs_semaine SET qte=" & app.EndTime.Hour - app.StartTime.Hour & _
                    ", dh_debut='" & Format(app.StartTime, "yyyy/MM/dd HH:mm") & "', cd_mon='" & pCD_MON & _
                    "', cd_uti='" & gUserCode & "' WHERE no_ligne=" & app.Id
        ExecQuery(ctDSN_AEM, szSQL)

    End Sub



End Class